package cn.itcast.demo.web;

import cn.itcast.demo.pojo.Account;
import cn.itcast.demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @GetMapping("/{id}")
    public Account findById(@PathVariable("id") Long id){
        return accountService.findById(id);
    }

    /**
     * 转账业务
     * @param from 转出账户id
     * @param to 转入账户id
     * @param amount 转出金额
     * @return
     */
    @PutMapping("transfer")
    public String transfer(
            @RequestParam("from") Long from, @RequestParam("to") Long to, @RequestParam("amount") Long amount){
        // 调用service业务
        accountService.transfer(from, to, amount);
        // 返回结果
        return "转账成功";
    }

    @GetMapping("list")
    public ModelAndView listAccount(){
        // 创建视图对象
        ModelAndView mv = new ModelAndView("list");
        // 存放模型数据
        mv.addObject("msg", "hello, springBoot!");
        List<Account> list = new ArrayList<>();
        list.add(new Account(1L, "虎哥", 1000L));
        list.add(new Account(2L, "虎妹", 1000L));
        list.add(new Account(3L, "虎姐", 1000L));
        mv.addObject("list", list);
        // 返回
        return mv;
    }
}
